﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using ComputerVision.Common;

namespace ComputerVision
{
    public partial class frmConfigDatabase : Form
    {
        private SqlConnection connecttion = null;
        private string strCon = "";
        private SqlConnectionStringBuilder builder;
        private string connectionstring = "";
        public frmConfigDatabase()
        {
            InitializeComponent();
            connectionstring = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            builder = new SqlConnectionStringBuilder(connectionstring);

            txtServer.Text = builder.DataSource;
            txtPassword.Text = builder.Password;
            txtDatabase.Text = builder.InitialCatalog;
            txtUserId.Text = builder.UserID;
        }

        private void btCheck_Click(object sender, EventArgs e)
        {
            StringBuilder Con = new StringBuilder("Data Source=");
            Con.Append(txtServer.Text.Trim());
            Con.Append(";Initial Catalog=");
            Con.Append(txtDatabase.Text.Trim());
            Con.Append(";User ID=");
            Con.Append(txtUserId.Text.Trim());
            Con.Append(";password=");
            Con.Append(txtPassword.Text.Trim());
            Con.Append(";");

            strCon = Con.ToString();

            if (Connect(strCon))
            {
                AlertList.Information("Kết nối dữ liệu thành công. Hãy lưu lại kết nối.");
                btSave.Enabled = true;
            }
            else
            {
                btSave.Enabled = false;
                AlertList.Error("Kết nối dữ liệu thất bại. Xin vui lòng kiểm tra lại các thông tin nhập vào.");
            }
        }

        private bool Connect(string connect)
        {
            connecttion = new SqlConnection(connect);
            try
            {
                connecttion.Open();
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show("Error:" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            finally
            {
                if (connecttion != null && connecttion.State != ConnectionState.Closed)
                    connecttion.Close();
            }
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            try
            {
                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
                connectionStringsSection.ConnectionStrings["DefaultConnection"].ConnectionString = strCon;
                connectionStringsSection.ConnectionStrings["ComputerVisionContext"].ConnectionString = @"metadata=res://*/ComputerVision.csdl|res://*/ComputerVision.ssdl|res://*/ComputerVision.msl;provider=System.Data.SqlClient;provider connection string='" + strCon + @"MultipleActiveResultSets=True;App=EntityFramework'";

                config.Save();
                ConfigurationManager.RefreshSection("connectionStrings");

                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
